package com.example.segment_saver;
/*
 * Nov 16th 2012
 * Created By: Alex Drotar, Jacob Reiger 
 * Using code from: Ken Anderson 
 * Segment_Saver- This is meant to be run on Andriod mobile devices
 * 					The app will allow the user to take in a url that they
 * 					want to watch. If the user selects the update site button
 * 					The application will go and check if the site has changed 
 * 					since the last time the user looked at it.
 * 
 */

import java.io.File;
import java.util.Iterator;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener, OnItemClickListener {

	private UpdateSites update_sites_task;
	//private Settings settings_task;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item);
        adapter.add("No Updated Sites");
        
        ListView filters = (ListView) findViewById(R.id.sites);
        filters.setAdapter(adapter);
        
        Button get_button = (Button) findViewById(R.id.update_sites);
        Button settings_button = (Button) findViewById(R.id.settings);
        
        get_button.setOnClickListener(this);
        settings_button.setOnClickListener(this);
        filters.setOnItemClickListener(this);
        
    }
    
    @Override
	public void onClick(View v){
    	switch(v.getId())
    	{
    	case	R.id.update_sites:
    		update_sites_task = new UpdateSites();
    		update_sites_task.execute();
    			break;
    	case	R.id.settings:
    		Intent intent = new Intent(this, Delete_Site.class);
    		startActivity(intent);
    			break;
    		
    	case R.id.sites:
    		
    		Log.i("Segment Saver", "Sites Clicked");
    		//settings_task = new Settings();
    		//settings_task.execute();
    			// Can add more if we have a different button like change the update sites
    	}
    }

    
    

    private class UpdateSites extends AsyncTask<Object, String, Boolean> {
    	private ProgressDialog progressDialog;
    	private SiteList sites;
    	@Override
    	protected void onPreExecute() {
    		// This is a Dialog Box that appears While it works. 
    		progressDialog = ProgressDialog.show(MainActivity.this, "Checking Sites for Updates", "Updating Site List ...", true, true);
    		
    	}
    	@Override
    	protected void onPostExecute(Boolean result) {
    		// This closes the Dialog Box that it opens. 
    		progressDialog.dismiss();
      	}
    	@Override
    	protected Boolean doInBackground(Object... params)
    	{
    		sites = new SiteList();
    		sites.load(new File(MainActivity.this.getFilesDir(), "sites.bin"));
    		String[] list = new String[sites.size()];// This has to be allocated to the exact size of strings that we want
    		
    		Iterator<String> iter = sites.iterator();
    		SiteData temp = new SiteData();
    		
    		for(int i = 0; iter.hasNext(); i++)
    		{
    			temp = sites.getSite(iter.next());
    			
    			if(temp.doSitesDiffer())
    			{
    				list[i] = temp.getUrl();
    				Log.i("Segment Saver", "Sites Differ");
    			}
    			else
    			{
    				i--;
    			}
    			
    			sites.save(new File(MainActivity.this.getFilesDir(), "sites.bin"));
    		}
    		publishProgress(list);
    		return true;
    	}
    	
    	
    	
    	@Override
		protected void onProgressUpdate(String... values){
    		ArrayAdapter<SpannableString> adapter = new ArrayAdapter<SpannableString>(MainActivity.this,R.layout.list_item);
    	
    		for(String site : values){
    			if(site != null)
    			{
	    			SpannableString temp = new SpannableString(site);
	    			Linkify.addLinks(temp,Linkify.WEB_URLS);
	    			adapter.add(temp);
	    			//String text = "<a href="+site+"</a>";
	        		//textcontent.setText(Html.fromHtml(text));
    			}
    		}
    		
    		ListView sites = (ListView) MainActivity.this.findViewById(R.id.sites);
    		sites.setAdapter(adapter);

    	}
    	
    	
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		//AdapterView<?> parent, View view, int position, long id
		
		TextView test = (TextView)arg1;
		
		Log.i("Segment Saver", test.getText().toString());
		Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(test.getText().toString()));
		startActivity(browserIntent);
		/*
		 TextView text = (TextView) getListView().getChildAt(itemSelected);
         remove_site(text.getText().toString());
         Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(text.getText().toString()));
         startActivity(browserIntent);
		*/
	}

	
}
